﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Service.Valid.StoreValid;
using S4Store.Domain.AfterSaleKpi;
using S4Store.Domain.AfterSaleReport;
namespace S4Store.Service
{
    public class AfterSaleCarKPIService : BaseService
    {

        public IQueryable<AfterSaleCarTypeDayInput> GetAfterSaleCarKpiDayDataQuery()
        {
            return this.GetQuery<AfterSaleCarTypeDayInput>();
        }
        public IQueryable<AfterSaleCarTypeMonthConfig> GetAfterSaleCarKpiMonthDataQuery()
        {
            return this.GetQuery<AfterSaleCarTypeMonthConfig>();
        }

        public void ModifyAfterSaleCarTypeKPIMonthConfig(AfterSaleCarTypeMonthConfig model)
        {
            AfterSaleCarTypeMonthConfig mymodel = this.GetQuery<AfterSaleCarTypeMonthConfig>().Where(o => o.CarBrandId == model.CarBrandId).FirstOrDefault();
            if (mymodel == null)
            {
                this.GetSet<AfterSaleCarTypeMonthConfig>().Add(model);
            }
        }

        public void SubmitAfterSaleCarTypeMonthConfigList(int year, int month, int storeid, IList<AfterSaleCarTypeMonthConfig> list)
        {
            var CarModelMonthConfigs = this.GetQuery<AfterSaleCarTypeMonthConfig>().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            //var confirms = CarModelMonthConfigs.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var Carc in CarModelMonthConfigs)
            {
                this.GetSet<AfterSaleCarTypeMonthConfig>().Remove(Carc);
            }
            foreach (var config in list)
            {
                this.GetSet<AfterSaleCarTypeMonthConfig>().Add(config);
            }
        }

        public void SubmitAfterSaleCarTypeDayDataList(int year, int month,int day, int storeid, IList<AfterSaleCarTypeDayInput> list)
        {
            var query = this.GetQuery<AfterSaleCarTypeDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day==day && t.StoreId == storeid);
            //var confirms = query.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var item in query)
            {
                this.GetSet<AfterSaleCarTypeDayInput>().Remove(item);
            }
            foreach (var item in list)
            {
                this.GetSet<AfterSaleCarTypeDayInput>().Add(item);
            }
        }

        public List<AfterSaleCarTypeMonthConfig> GetAfterSaleCarTypeMonthConfigs(int year, int month, int storeid)
        {
            var CarModelMonthConfigs = this.GetQuery<AfterSaleCarTypeMonthConfig>().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            return CarModelMonthConfigs.ToList();
        }

        public List<AfterSaleCarTypeDayInput> GetAfterSaleCarTypeDayInputs(int year, int month, int day, int storeid)
        {
            var afterSaleCarTypeDayInputs = this.GetQuery<AfterSaleCarTypeDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day == day && t.StoreId == storeid);
            return afterSaleCarTypeDayInputs.ToList();
        }

        public List<AfterSaleCarTypeDayInput> GetAfterSaleCarTypeMonthFinished(int year, int month,int storeid)
        {
            var afterSaleCarTypeDayInputs = this.GetQuery<AfterSaleCarTypeDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.StoreId == storeid);
            return afterSaleCarTypeDayInputs.ToList();
        }


        #region 售后装潢KPI
        public List<AfterSaleCarDecoratingMonthConfig> GetAfterSaleCarDecoratingMonthConfigs(int year, int month,int storeid)
        {
            var query = this.GetQuery<AfterSaleCarDecoratingMonthConfig>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.StoreId == storeid);
            return query.ToList();
        }

        /// <summary>
        /// 日数据
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="d"></param>
        /// <param name="storeid"></param>
        /// <returns></returns>
        public List<AfterSaleCarDecoratingDayInput> GetAfterSaleCarDecoratingDayInputs(int year, int month, int d, int storeid)
        {
            var query = this.GetQuery<AfterSaleCarDecoratingDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day==d && t.StoreId == storeid);
            return query.ToList();
        }
        public List<AfterSaleCarDecoratingDayInput> GetAfterSaleCarDecoratingDayInputs(int year, int month, int storeid)
        {
            var query = this.GetQuery<AfterSaleCarDecoratingDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.StoreId == storeid);
            return query.ToList();
        }
        public List<AfterSaleCarDecoratingDayInput> GetAfterSaleCarDecoratingDayInputsQuery()
        {
            var query = this.GetQuery<AfterSaleCarDecoratingDayInput>();
            return query.ToList();
        }

        public void SubmitAfterSaleCarDecoratingMonthConfigList(int year, int month, int storeid, IList<AfterSaleCarDecoratingMonthConfig> list)
        {
            var query = this.GetQuery<AfterSaleCarDecoratingMonthConfig>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.StoreId == storeid);
            //var confirms = query.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var item in query)
            {
                this.GetSet<AfterSaleCarDecoratingMonthConfig>().Remove(item);
            }
            foreach (var config in list)
            {
                this.GetSet<AfterSaleCarDecoratingMonthConfig>().Add(config);
            }
        }

        public void SubmitAfterSaleCarDecoratingDayDataList(int year, int month, int day, int storeid, IList<AfterSaleCarDecoratingDayInput> list)
        {
            var query = this.GetQuery<AfterSaleCarDecoratingDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day == day && t.StoreId == storeid);
            //var confirms = query.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var item in query)
            {
                this.GetSet<AfterSaleCarDecoratingDayInput>().Remove(item);
            }
            foreach (var item in list)
            {
                this.GetSet<AfterSaleCarDecoratingDayInput>().Add(item);
            }
        }

        public IQueryable<AfterSaleCarDecoratingDayInput> GetAfterSaleCarDecoratingDayDataQuery()
        {
            return this.GetQuery<AfterSaleCarDecoratingDayInput>();
        }
        public IQueryable<AfterSaleCarDecoratingMonthConfig> AfterSaleCarDecoratingMonthConfigQuery()
        {
            return this.GetQuery<AfterSaleCarDecoratingMonthConfig>();
        }

        #endregion


        #region 服务顾问

        public IQueryable<AfterSaleServiceAdvisorDayInput> GetAfterSaleServiceAdvisorDayInputQuery()
        {
            return this.GetQuery<AfterSaleServiceAdvisorDayInput>();
        }

        public IQueryable<AfterSaleServiceAdvisorMonthTarget> GetAfterSaleServiceAdvisorMonthTargetQuery()
        {
            return this.GetQuery<AfterSaleServiceAdvisorMonthTarget>();
        }

        public AfterSaleServiceAdvisorMonthConfig GetAfterSaleServiceAdvisorMonthConfig(int y, int m, int storeid)
        {
            var query = this.GetQuery<AfterSaleServiceAdvisorMonthConfig>().Where(t => t.AddTime.Year == y && t.AddTime.Month == m && t.StoreId == storeid);
            return query.FirstOrDefault();
        }

        public List<AfterSaleServiceAdvisorMonthTarget> GetAfterSaleServiceAdvisorMonthTargets(int year, int month, int[] employees)
        {
            var query = this.GetQuery<AfterSaleServiceAdvisorMonthTarget>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && employees.Contains(t.EmployeeId));
            return query.ToList();
        }

        public List<AfterSaleServiceAdvisorMonthTarget> GetAfterSaleServiceAdvisorMonthTargetsQuery()
        {
            var query = this.GetQuery<AfterSaleServiceAdvisorMonthTarget>();
            return query.ToList();
        }

        public void SubmitServiceAdvisorMonthConfig(int year, int month, int storeid , AfterSaleServiceAdvisorMonthConfig config)
        {
            var query = this.GetQuery<AfterSaleServiceAdvisorMonthConfig>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.StoreId==storeid).FirstOrDefault();
            //if (query != null && query.IsConfirmed) throw new Exception("确认过的数据，不能再修改！");
            if(query!=null)
            {
                this.GetSet<AfterSaleServiceAdvisorMonthConfig>().Remove(query);
            }
            this.GetSet<AfterSaleServiceAdvisorMonthConfig>().Add(config);
        }

        public void SubmitServiceAdvisorMonthTargetsList(int year, int month, int[] employs, List<AfterSaleServiceAdvisorMonthTarget> list)
        {
            var query = this.GetQuery<AfterSaleServiceAdvisorMonthTarget>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && employs.Contains(t.EmployeeId));
            //var confirms = query.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var item in query)
            {
                this.GetSet<AfterSaleServiceAdvisorMonthTarget>().Remove(item);
            }
            foreach (var config in list)
            {
                this.GetSet<AfterSaleServiceAdvisorMonthTarget>().Add(config);
            }
        }

        public void SubmitServiceAdvisorDayInputsList(int year, int month,int d, int[] employs, List<AfterSaleServiceAdvisorDayInput> list)
        {
            var query = this.GetQuery<AfterSaleServiceAdvisorDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day==d && employs.Contains(t.EmployeeId));
            //var confirms = query.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var item in query)
            {
                this.GetSet<AfterSaleServiceAdvisorDayInput>().Remove(item);
            }
            foreach (var config in list)
            {
                this.GetSet<AfterSaleServiceAdvisorDayInput>().Add(config);
            }
        }

        public List<AfterSaleServiceAdvisorDayInput> GetAfterSaleServiceAdvisorDayInputs(int year, int month, int day,int[] employees)
        {
            var query = this.GetQuery<AfterSaleServiceAdvisorDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day==day && employees.Contains(t.EmployeeId));
            return query.ToList();
        }



        public List<AfterSaleServiceAdvisorDayInput> GetAfterSaleServiceAdvisorDayInputs(int year, int month,  int[] employees)
        {
            var query = this.GetQuery<AfterSaleServiceAdvisorDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && employees.Contains(t.EmployeeId));
            return query.ToList();
        }

        #endregion

        #region 车间主管

        public IQueryable<AfterSaleWorkshopManagerDayInput> GetAfterSaleWorkshopManagerDayInputQuery()
        {
            return this.GetQuery<AfterSaleWorkshopManagerDayInput>();
        }

        public List<AfterSaleWorkshopManagerMonthConfig> GetAfterSaleWorkshopManagerMonthConfigs(int year, int month, int[] employees)
        {
            var query = this.GetQuery<AfterSaleWorkshopManagerMonthConfig>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && employees.Contains(t.EmployeeId));
            return query.ToList();
        }

        public List<AfterSaleWorkshopManagerMonthConfig> GetAfterSaleWorkshopManagerMonthConfigs(int[] employees)
        {
            var query = this.GetQuery<AfterSaleWorkshopManagerMonthConfig>().Where(t=>employees.Contains(t.EmployeeId));
            return query.ToList();
        }

        public void SubmitWorkshopManagerMonthConfigsList(int year, int month, int[] employs, List<AfterSaleWorkshopManagerMonthConfig> list)
        {
            var query = this.GetQuery<AfterSaleWorkshopManagerMonthConfig>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && employs.Contains(t.EmployeeId));
            //var confirms = query.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var item in query)
            {
                this.GetSet<AfterSaleWorkshopManagerMonthConfig>().Remove(item);
            }
            foreach (var config in list)
            {
                this.GetSet<AfterSaleWorkshopManagerMonthConfig>().Add(config);
            }
        }

        public void SubmitWorkshopManagerDayInputsList(int year, int month, int d, int[] employs, List<AfterSaleWorkshopManagerDayInput> list)
        {
            var query = this.GetQuery<AfterSaleWorkshopManagerDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day == d && employs.Contains(t.EmployeeId));
            //var confirms = query.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var item in query)
            {
                this.GetSet<AfterSaleWorkshopManagerDayInput>().Remove(item);
            }
            foreach (var config in list)
            {
                this.GetSet<AfterSaleWorkshopManagerDayInput>().Add(config);
            }
        }

        public List<AfterSaleWorkshopManagerDayInput> GetAfterSaleWorkshopManagerDayInputs(int year, int month, int day, int[] employees)
        {
            var query = this.GetQuery<AfterSaleWorkshopManagerDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day == day && employees.Contains(t.EmployeeId));
            return query.ToList();
        }
        public List<AfterSaleWorkshopManagerDayInput> GetAfterSaleWorkshopManagerDayInputs(int year, int month, int[] employees)
        {
            var query = this.GetQuery<AfterSaleWorkshopManagerDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && employees.Contains(t.EmployeeId));
            return query.ToList();
        }

        #endregion

        #region 客户专员
        public List<AfterSaleCustomerServiceDayInput> GetAfterSaleCustomerServiceDayInputs(int year, int month, int day, int[] employees)
        {
            var query = this.GetQuery<AfterSaleCustomerServiceDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day==day && employees.Contains(t.EmployeeId));
            return query.ToList();
        }

        public List<AfterSaleCustomerServiceDayInput> GetAfterSaleCustomerServiceDayInputs(int year, int month, int[] employees)
        {
            var query = this.GetQuery<AfterSaleCustomerServiceDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && employees.Contains(t.EmployeeId));
            return query.ToList();
        }

        public void SubmitCustomerServiceDayInputsList(int year, int month, int d, int[] employs, List<AfterSaleCustomerServiceDayInput> list)
        {
            var query = this.GetQuery<AfterSaleCustomerServiceDayInput>().Where(t => t.AddTime.Year == year && t.AddTime.Month == month && t.AddTime.Day == d && employs.Contains(t.EmployeeId));
            //var confirms = query.Where(o => o.IsConfirmed);
            //if (confirms != null && confirms.Count() > 0) throw new Exception("确认过的数据，不能再修改！");
            foreach (var item in query)
            {
                this.GetSet<AfterSaleCustomerServiceDayInput>().Remove(item);
            }
            foreach (var config in list)
            {
                this.GetSet<AfterSaleCustomerServiceDayInput>().Add(config);
            }
        }
        public IQueryable<AfterSaleCustomerServiceDayInput> GetAfterSaleCustomerServiceDayInputQuery()
        {
            return this.GetQuery<AfterSaleCustomerServiceDayInput>();
        }

        #endregion

        #region 月初配置
        public MonthStandardConfig GetAfterSaleMonthStandardConfig(int y, int m, int storeid)
        {
            var query = this.GetQuery<MonthStandardConfig>().Where(t => t.Month.Year == y && t.Month.Month == m && t.StoreId == storeid);
            return query.FirstOrDefault();
        }

        public IQueryable<MonthStandardConfig> GetAfterSaleMonthStandardConfigs()
        {
            return this.GetQuery<MonthStandardConfig>();
        }

        public List<MonthStandardConfig> GetAfterSaleMonthStandardConfigs(int storeid)
        {
            return  this.GetQuery<MonthStandardConfig>().Where(t=>t.StoreId == storeid).ToList();
        }

        public List<MonthStandardConfig> GetAfterSaleMonthStandardConfigs(int y, int m, int storeid)
        {
            return this.GetQuery<MonthStandardConfig>().Where(t => t.Month.Year == y && t.Month.Month == m && t.StoreId == storeid).ToList();
        }

        public void SubmitSaleMonthStandardConfig(int year, int month, int storeid, MonthStandardConfig config)
        {
            var query = this.GetQuery<MonthStandardConfig>().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid).FirstOrDefault();
            //if (query != null && query.IsConfirmed) throw new Exception("确认过的数据，不能再修改！");
            if (query != null)
            {
                this.GetSet<MonthStandardConfig>().Remove(query);
            }
            this.GetSet<MonthStandardConfig>().Add(config);
        }


        #endregion


        #region report
        public CarTypeReport GetCarTypeReport(int year, int month, int storeId, int brandid = 0)
        {
            List<AfterSaleCarTypeMonthConfig> M = GetAfterSaleCarTypeMonthConfigs(year, month, storeId)
                .Where(o=>o.CarBrandId==brandid && o.IsConfirmed).ToList();
            if(brandid==0)
                M = GetAfterSaleCarTypeMonthConfigs(year, month, storeId).Where(o => o.IsConfirmed).ToList();

            if (M == null || M.Count==0)
            {
                M = new List<AfterSaleCarTypeMonthConfig>();
            }

            List<AfterSaleCarTypeDayInput> D = GetAfterSaleCarTypeMonthFinished(year, month, storeId)
                .Where(o => o.CarBrandId == brandid && o.IsConfirmed).ToList();
            if(brandid==0)
               D = GetAfterSaleCarTypeMonthFinished(year, month, storeId).Where(o => o.IsConfirmed).ToList();

            if (D == null || D.Count == 0)
            {
                D = new List<AfterSaleCarTypeDayInput>();
            }

            CarTypeReport ctr = new CarTypeReport();
            ctr.M = M;
            ctr.D = D;

            return ctr;
        }

        public CarDecoReport GetCarDecoReport(int year, int month, int storeId, int brandid = 0)
        {
            List<AfterSaleCarDecoratingMonthConfig> M = GetAfterSaleCarDecoratingMonthConfigs(year, month, storeId)
                .Where(o=>o.CarBrandId==brandid && o.IsConfirmed).ToList();
            if (brandid == 0)
                M = GetAfterSaleCarDecoratingMonthConfigs(year, month, storeId).Where(o => o.IsConfirmed).ToList();

            if (M == null || M.Count == 0)
            {
                M = new List<AfterSaleCarDecoratingMonthConfig>();
            }

            List<AfterSaleCarDecoratingDayInput> D = GetAfterSaleCarDecoratingDayInputs(year,month,storeId)
                .Where(o => o.CarBrandId == brandid && o.IsConfirmed).ToList();
            if (brandid == 0)
                D = GetAfterSaleCarDecoratingDayInputs(year, month, storeId).Where(o => o.IsConfirmed).ToList();

            if (D == null || D.Count == 0)
            {
                D = new List<AfterSaleCarDecoratingDayInput>();
            }

            CarDecoReport ctr = new CarDecoReport();
            ctr.M = M;
            ctr.D = D;

            return ctr;
        }

        public SAReport GetSAReport(int year, int month, int[] employids)
        {
            List<AfterSaleServiceAdvisorMonthTarget> M = GetAfterSaleServiceAdvisorMonthTargets(year,month,employids)
                .Where(o => o.IsConfirmed).ToList();
            if (M == null || M.Count == 0)
            {
                M = new List<AfterSaleServiceAdvisorMonthTarget>();
            }

            List<AfterSaleServiceAdvisorDayInput> D = GetAfterSaleServiceAdvisorDayInputs(year, month, employids)
                .Where(o => o.IsConfirmed).ToList();
            if (D == null || D.Count == 0)
            {
                D = new List<AfterSaleServiceAdvisorDayInput>();
            }

            SAReport ctr = new SAReport();
            ctr.M = M;
            ctr.D = D;

            return ctr;
        }

        public WMReport GetWMReport(int year, int month, int[] employids,int storeid=0)
        {
            List<AfterSaleWorkshopManagerMonthConfig> M = GetAfterSaleWorkshopManagerMonthConfigs(year, month, employids)
                .Where(o => o.IsConfirmed).ToList();
            if (M == null || M.Count == 0)
            {
                M = new List<AfterSaleWorkshopManagerMonthConfig>();
            }

            List<AfterSaleWorkshopManagerDayInput> D = GetAfterSaleWorkshopManagerDayInputs(year, month, employids)
                .Where(o => o.IsConfirmed).ToList();
            if (D == null || D.Count == 0)
            {
                D = new List<AfterSaleWorkshopManagerDayInput>();
            }

            Domain.AfterSaleKpi.MonthStandardConfig S = GetAfterSaleMonthStandardConfig(year, month, storeid);
            if (S == null)
            {
                S = new MonthStandardConfig();
            }

            WMReport ctr = new WMReport();
            ctr.M = M;
            ctr.D = D;
            ctr.StandardConfig = S;

            return ctr;
        }

        public CSReport GetCSReport(int year, int month, int[] csemployids, int[] saemployids, int storeid = 0)
        {
            List<AfterSaleCustomerServiceDayInput> CD = GetAfterSaleCustomerServiceDayInputs(year, month, csemployids)
                .Where(o => o.IsConfirmed).ToList();
            if (CD == null || CD.Count == 0)
            {
                CD = new List<AfterSaleCustomerServiceDayInput>();
            }

            List<AfterSaleServiceAdvisorMonthTarget> M = GetAfterSaleServiceAdvisorMonthTargets(year, month, saemployids)
                .Where(o => o.IsConfirmed).ToList();
            if (M == null || M.Count == 0)
            {
                M = new List<AfterSaleServiceAdvisorMonthTarget>();
            }

            List<AfterSaleServiceAdvisorDayInput> D = GetAfterSaleServiceAdvisorDayInputs(year, month, saemployids)
                .Where(o => o.IsConfirmed).ToList();
            if (D == null || D.Count == 0)
            {
                D = new List<AfterSaleServiceAdvisorDayInput>();
            }


            Domain.AfterSaleKpi.MonthStandardConfig S = GetAfterSaleMonthStandardConfig(year, month, storeid);
            if (S == null)
            {
                S = new MonthStandardConfig();
            }

            CSReport ctr = new CSReport();
            ctr.M = M;
            ctr.D = D;
            ctr.CD = CD;
            ctr.StandardConfig = S;

            return ctr;
        }
        #endregion

    }
}
